package com.china.produce.cglib;

import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

/**
 *
 *cglib拦截器interceptor
 *
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2022/5/28
 */
public class TestInterceptor {

    /**
     * 创建拦截器
     *
     * @param args
     */
    public static void main(String[] args) {
        //设置这个属性，将代理类的字节码文件生成到目录下
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/niujie/Desktop/Work/springboot-action/");
        //实例化一个增强器，也就是cglib中的一个class generator
        Enhancer enhancer = new Enhancer();
        //设置目标类
        enhancer.setSuperclass(Target.class);
        //设置拦截对象
        MethodInterceptor interceptor = new Interceptor();
        enhancer.setCallback(interceptor);
        //生成代理类并返回一个实例
        Target t = (Target) enhancer.create();
        t.f();
        t.g();
        t.c();
    }
}
